package chenhehong;
import com.cloudgarden.layout.AnchorLayout;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.SwingUtilities;

import com.sun.prism.paint.Color;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
/**
 * 
 * @author ChenHehong
 * 主程序
 */
public class MainFrame extends javax.swing.JFrame {
	
	int MAXSIZE = 20; //系统允许最大进程数
	int USESIZE = 0; //系统中已装入的
	Pcb[] pcbarea = null; //定义进程控制块区域数组
	ArrayList bar = new ArrayList();
	private JProgressBar jProgressBar10;
	private JProgressBar jProgressBar9;
	private JProgressBar jProgressBar8;
	private JProgressBar jProgressBar7;
	private JProgressBar jProgressBar6;
	private JProgressBar jProgressBar5;
	private JProgressBar jProgressBar4;
	private JProgressBar jProgressBar3;
	private JProgressBar jProgressBar2;
	private JProgressBar jProgressBar1;
	private JPanel jPanel1;
	private JTable pcb_table;
	private JScrollPane jScrollPane1;
	int  run; //定义正在运行的进程控制块的指针
	int  head; //定义指向就绪队列的头指针
	int  tail; //定义指向就绪队列的尾指针
	int  pfree; //定义指向空闲进程控制块队列的指针
	private JRadioButton jRadioButton1;
	private JLabel jLabel8;
	private JPanel jPanel5;
	private JButton jButton3;
	private JButton jButton2;
	private JLabel jLabel7;
	private JLabel jLabel6;
	private JLabel jLabel5;
	private JLabel jLabel4;
	private JPanel jPanel4;
	private JButton jButton1;
	private JTextField jTextField3;
	private JLabel jLabel3;
	private JTextField jTextField2;
	private JLabel jLabel2;
	private JTextField jTextField1;
	private JLabel jLabel1;
	private JPanel jPanel3;
	private JRadioButton jRadioButton4;
	private JRadioButton jRadioButton3;
	private JRadioButton jRadioButton2;
	private ButtonGroup buttonGroup1;
	private JPanel jPanel2;
	private JProgressBar jProgressBar15;
	private JProgressBar jProgressBar14;
	private JProgressBar jProgressBar13;
	private JProgressBar jProgressBar12;
	private JProgressBar jProgressBar11;
	int  new_name; //定义系统中分配的最新进程标识符
	int  choice_lock; //锁定调度选择按钮
	int  ready_num;  //就绪的进程数
	int  manage_mode=1;  //调度方式，默认选择时间片轮转

	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				MainFrame inst = new MainFrame();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	public MainFrame() {
		super();
		initGUI();
		initializeBar();
		getButtonGroup1();
		randomProcess();
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			this.setTitle("fdgdf");
			{
				jScrollPane1 = new JScrollPane();
				getContentPane().add(jScrollPane1, "Center");
				jScrollPane1.setBounds(0, 0, 460, 320);
				jScrollPane1.setBorder(BorderFactory.createTitledBorder("就绪队列"));
				{
					TableModel pcb_tableModel = 
							new DefaultTableModel(
									new String[] { "进程标识符", "进程优先数", "总需求时间", "剩余时间", "进程状态" },0);
					pcb_table = new JTable();
					jScrollPane1.setViewportView(pcb_table);
					pcb_table.setModel(pcb_tableModel);
				}
			}
			{
				jPanel1 = new JPanel();
				GroupLayout jPanel1Layout = new GroupLayout((JComponent)jPanel1);
				jPanel1.setLayout(jPanel1Layout);
				getContentPane().add(jPanel1);
				jPanel1.setBounds(466, 0, 177, 329);
				{
					jProgressBar1 = new JProgressBar();
				}
				{
					jProgressBar2 = new JProgressBar();
				}
				{
					jProgressBar3 = new JProgressBar();
				}
				{
					jProgressBar4 = new JProgressBar();
				}
				{
					jProgressBar5 = new JProgressBar();
				}
				{
					jProgressBar6 = new JProgressBar();
				}
				{
					jProgressBar7 = new JProgressBar();
				}
				{
					jProgressBar8 = new JProgressBar();
				}
				{
					jProgressBar9 = new JProgressBar();
				}
				{
					jProgressBar10 = new JProgressBar();
				}
				{
					jProgressBar11 = new JProgressBar();
				}
				{
					jProgressBar12 = new JProgressBar();
				}
				{
					jProgressBar13 = new JProgressBar();
				}
				{
					jProgressBar14 = new JProgressBar();
				}
				{
					jProgressBar15 = new JProgressBar();
				}
				jPanel1Layout.setHorizontalGroup(jPanel1Layout.createSequentialGroup()
					.addGroup(jPanel1Layout.createParallelGroup()
					    .addComponent(jProgressBar1, GroupLayout.Alignment.LEADING, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
					    .addGroup(jPanel1Layout.createSequentialGroup()
					        .addComponent(jProgressBar2, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					        .addGap(0, 0, Short.MAX_VALUE))
					    .addGroup(jPanel1Layout.createSequentialGroup()
					        .addComponent(jProgressBar3, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					        .addGap(0, 0, Short.MAX_VALUE))
					    .addGroup(jPanel1Layout.createSequentialGroup()
					        .addComponent(jProgressBar4, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					        .addGap(0, 0, Short.MAX_VALUE))
					    .addGroup(jPanel1Layout.createSequentialGroup()
					        .addComponent(jProgressBar5, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					        .addGap(0, 0, Short.MAX_VALUE))
					    .addGroup(jPanel1Layout.createSequentialGroup()
					        .addComponent(jProgressBar6, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					        .addGap(0, 0, Short.MAX_VALUE))
					    .addGroup(jPanel1Layout.createSequentialGroup()
					        .addComponent(jProgressBar7, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					        .addGap(0, 0, Short.MAX_VALUE))
					    .addGroup(jPanel1Layout.createSequentialGroup()
					        .addComponent(jProgressBar8, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					        .addGap(0, 0, Short.MAX_VALUE))
					    .addGroup(jPanel1Layout.createSequentialGroup()
					        .addComponent(jProgressBar9, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					        .addGap(0, 0, Short.MAX_VALUE))
					    .addGroup(jPanel1Layout.createSequentialGroup()
					        .addComponent(jProgressBar10, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					        .addGap(0, 0, Short.MAX_VALUE))
					    .addGroup(jPanel1Layout.createSequentialGroup()
					        .addComponent(jProgressBar11, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					        .addGap(0, 0, Short.MAX_VALUE))
					    .addGroup(jPanel1Layout.createSequentialGroup()
					        .addComponent(jProgressBar12, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					        .addGap(0, 0, Short.MAX_VALUE))
					    .addGroup(jPanel1Layout.createSequentialGroup()
					        .addComponent(jProgressBar13, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					        .addGap(0, 0, Short.MAX_VALUE))
					    .addGroup(jPanel1Layout.createSequentialGroup()
					        .addComponent(jProgressBar14, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					        .addGap(0, 0, Short.MAX_VALUE))
					    .addGroup(GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
					        .addComponent(jProgressBar15, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					        .addGap(0, 0, Short.MAX_VALUE)))
					.addContainerGap(29, 29));
				jPanel1Layout.setVerticalGroup(jPanel1Layout.createSequentialGroup()
					.addContainerGap(24, 24)
					.addComponent(jProgressBar1, GroupLayout.PREFERRED_SIZE, 16, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
					.addComponent(jProgressBar2, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
					.addComponent(jProgressBar3, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
					.addComponent(jProgressBar4, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
					.addComponent(jProgressBar5, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
					.addComponent(jProgressBar6, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
					.addComponent(jProgressBar7, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
					.addComponent(jProgressBar8, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
					.addComponent(jProgressBar9, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
					.addComponent(jProgressBar10, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
					.addComponent(jProgressBar11, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
					.addComponent(jProgressBar12, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
					.addComponent(jProgressBar13, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
					.addComponent(jProgressBar14, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
					.addComponent(jProgressBar15, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
			}
			{
				jPanel2 = new JPanel();
				FlowLayout jPanel2Layout = new FlowLayout();
				jPanel2Layout.setAlignment(FlowLayout.LEFT);
				jPanel2.setLayout(jPanel2Layout);
				getContentPane().add(jPanel2);
				getContentPane().add(getJPanel3());
				getContentPane().add(getJPanel4());
				getContentPane().add(getJPanel5());
				jPanel2.setBounds(6, 332, 158, 141);
				jPanel2.setBorder(BorderFactory.createTitledBorder("调度选择"));
				{
					jRadioButton1 = new JRadioButton();
					jPanel2.add(jRadioButton1);
					jRadioButton1.setText("\u65f6\u95f4\u7247\u8f6e\u8f6c");
					jRadioButton1.setPreferredSize(new java.awt.Dimension(93, 21));
					jRadioButton1.setSelected(true);
				}
				{
					jRadioButton2 = new JRadioButton();
					jPanel2.add(jRadioButton2);
					jRadioButton2.setText("\u4f18\u5148\u6570\u8c03\u5ea6");
				}
				{
					jRadioButton3 = new JRadioButton();
					jPanel2.add(jRadioButton3);
					jRadioButton3.setText("\u6700\u77ed\u8fdb\u7a0b\u4f18\u5148");
				}
				{
					jRadioButton4 = new JRadioButton();
					jPanel2.add(jRadioButton4);
					jRadioButton4.setText("\u6700\u77ed\u5269\u4f59\u65f6\u95f4\u4f18\u5148");
				}
			}
			pack();
			this.setSize(659, 524);
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
		
	}
	
/**
 * 对进度条的初始化，设置为“不可见”，“前色为红”，“显示字符”
*/    
void initializeBar()
{
	bar.add(jProgressBar1);
	bar.add(jProgressBar2);
	bar.add(jProgressBar3);
	bar.add(jProgressBar4);
	bar.add(jProgressBar5);
	bar.add(jProgressBar6);
	bar.add(jProgressBar7);
	bar.add(jProgressBar8);
	bar.add(jProgressBar9);
	bar.add(jProgressBar10);
	bar.add(jProgressBar11);
	bar.add(jProgressBar12);
	bar.add(jProgressBar13);
	bar.add(jProgressBar14);
	bar.add(jProgressBar15);
	
	for(int i = 0 ; i < bar.size() ;i++)        
	{
	    ((JProgressBar) bar.get(i)).setVisible(false);
	    ((JProgressBar) bar.get(i)).setForeground(java.awt.Color.RED);
	    ((JProgressBar) bar.get(i)).setStringPainted(true);
	}
}
	
	public boolean initPcbarea(int size) {
		this.pcbarea = new Pcb[this.MAXSIZE];
		for (int i = 0; i < size; i++) {
			
		}
		return true;
	}
	
	private ButtonGroup getButtonGroup1() {
		if(buttonGroup1 == null) {
			buttonGroup1 = new ButtonGroup();
			buttonGroup1.add(jRadioButton1);
			buttonGroup1.add(jRadioButton2);
			buttonGroup1.add(jRadioButton3);
			buttonGroup1.add(jRadioButton4);
		}
		return buttonGroup1;
	}
	
	private JPanel getJPanel3() {
		if(jPanel3 == null) {
			jPanel3 = new JPanel();
			FlowLayout jPanel3Layout = new FlowLayout();
			jPanel3.setLayout(jPanel3Layout);
			jPanel3.setBounds(170, 332, 158, 141);
			jPanel3.setBorder(BorderFactory.createTitledBorder("添加进程"));
			jPanel3.add(getJLabel1());
			jPanel3.add(getJTextField1());
			jPanel3.add(getJLabel2());
			jPanel3.add(getJTextField2());
			jPanel3.add(getJLabel3());
			jPanel3.add(getJTextField3());
			jPanel3.add(getJButton1());
		}
		return jPanel3;
	}
	
	private JLabel getJLabel1() {
		if(jLabel1 == null) {
			jLabel1 = new JLabel();
			FlowLayout jLabel1Layout = new FlowLayout();
			jLabel1.setLayout(jLabel1Layout);
			jLabel1.setText("\u6807\u8bc6\u7b26");
		}
		return jLabel1;
	}
	
	private JTextField getJTextField1() {
		if(jTextField1 == null) {
			jTextField1 = new JTextField();
			jTextField1.setText("jTextField1");
			jTextField1.setSize(100, 24);
			jTextField1.setPreferredSize(new java.awt.Dimension(81, 24));
			jTextField1.setEditable(false);
		}
		return jTextField1;
	}
	
	private JLabel getJLabel2() {
		if(jLabel2 == null) {
			jLabel2 = new JLabel();
			jLabel2.setText("\u4f18\u5148\u7ea7");
		}
		return jLabel2;
	}
	
	private JTextField getJTextField2() {
		if(jTextField2 == null) {
			jTextField2 = new JTextField();
			jTextField2.setText("jTextField2");
			jTextField2.setPreferredSize(new java.awt.Dimension(81, 24));
		}
		return jTextField2;
	}
	
	private JLabel getJLabel3() {
		if(jLabel3 == null) {
			jLabel3 = new JLabel();
			jLabel3.setText("\u603b\u65f6\u95f4");
		}
		return jLabel3;
	}
	
	private JTextField getJTextField3() {
		if(jTextField3 == null) {
			jTextField3 = new JTextField();
			jTextField3.setText("jTextField3");
			jTextField3.setPreferredSize(new java.awt.Dimension(79, 24));
		}
		return jTextField3;
	}
	
	private JButton getJButton1() {
		if(jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setText("\u6dfb\u52a0\u8fdb\u7a0b");
		}
		return jButton1;
	}
	
	/**
	 * 随机生成进程
	 * 填充JtextFieled的值
	 */
	public void randomProcess(){
		int pri = (int) (Math.random() * 5);  //优先级, 范围为0~4
        int time = 1 + (int) (Math.random() * 15); //所需时间，范围为1~15
        jTextField1.setText(Integer.toString(this.new_name));
        jTextField2.setText(Integer.toString(pri));
        jTextField3.setText(Integer.toString(time));
        this.new_name++;
	}
	
	private JPanel getJPanel4() {
		if(jPanel4 == null) {
			jPanel4 = new JPanel();
			jPanel4.setLayout(null);
			jPanel4.setBounds(343, 335, 148, 138);
			jPanel4.setBorder(BorderFactory.createTitledBorder("状态控制"));
			jPanel4.add(getJLabel5());
			jPanel4.add(getJLabel4());
			jPanel4.add(getJLabel6());
			jPanel4.add(getJLabel7());
			jPanel4.add(getJButton2());
			jPanel4.add(getJButton3());
		}
		return jPanel4;
	}
	
	private JLabel getJLabel4() {
		if(jLabel4 == null) {
			jLabel4 = new JLabel();
			jLabel4.setText("0");
			jLabel4.setBounds(103, 19, 28, 24);
		}
		return jLabel4;
	}
	
	private JLabel getJLabel5() {
		if(jLabel5 == null) {
			jLabel5 = new JLabel();
			jLabel5.setText("\u5c31\u7eea\u8fdb\u7a0b\u6570");
			jLabel5.setBounds(11, 19, 80, 27);
		}
		return jLabel5;
	}
	
	private JLabel getJLabel6() {
		if(jLabel6 == null) {
			jLabel6 = new JLabel();
			jLabel6.setText("\u7a7a\u95f2\u8fdb\u7a0b\u6570");
			jLabel6.setBounds(11, 42, 80, 34);
		}
		return jLabel6;
	}
	
	private JLabel getJLabel7() {
		if(jLabel7 == null) {
			jLabel7 = new JLabel();
			jLabel7.setText("15");
			jLabel7.setBounds(103, 51, 29, 17);
		}
		return jLabel7;
	}
	
	private JButton getJButton2() {
		if(jButton2 == null) {
			jButton2 = new JButton();
			jButton2.setText("\u5f00\u59cb");
			jButton2.setBounds(29, 80, 87, 24);
		}
		return jButton2;
	}
	
	private JButton getJButton3() {
		if(jButton3 == null) {
			jButton3 = new JButton();
			jButton3.setText("\u91cd\u542f");
			jButton3.setBounds(29, 109, 87, 24);
		}
		return jButton3;
	}
	
	private JPanel getJPanel5() {
		if(jPanel5 == null) {
			jPanel5 = new JPanel();
			jPanel5.setLayout(null);
			jPanel5.setBounds(503, 335, 113, 138);
			jPanel5.setBorder(BorderFactory.createTitledBorder("时间"));
			jPanel5.add(getJLabel8());
		}
		return jPanel5;
	}
	
	private JLabel getJLabel8() {
		if(jLabel8 == null) {
			jLabel8 = new JLabel();
			jLabel8.setText("0");
			jLabel8.setFont(new java.awt.Font("宋体",0,72));
			jLabel8.setBounds(42, 19, 54, 94);
		}
		return jLabel8;
	}

}
